
Function InitLuaState()
	LuaState = luaL_newstate()
	luaL_openlibs(LuaState)
	
	lua_pushstring(LuaState, "BMXString")
	lua_setglobal (LuaState, "luaglobal")
	
	lua_register(LuaState, "setcolor", bmxsetcolor)
	lua_register(LuaState, "fillRect", bmxfillRect)
	lua_register(LuaState, "drawRect", bmxdrawRect)
	lua_register(LuaState, "drawsph", bmxdrawsph)
	lua_register(LuaState, "drawline", bmxline)
	lua_register(LuaState, "bmxprintf", bmxprintf)
	lua_register(LuaState, "alphaparticle", bmxparticle)
	lua_register(LuaState, "drawsprite", bmxdrawsprite)
	lua_register(LuaState, "drawAnimSprite", bmxdrawAnimSprite)
	lua_register(LuaState, "drawSparkler", bmxDrawSparkler)
	lua_register(LuaState, "setOrigin", bmxSetOrigin)
	lua_register(LuaState, "setScale", bmxSetScale)
	lua_register(LuaState, "moveto", DrawState.bmxMoveTo)
	lua_register(LuaState, "lineto", bmxLineTo)
	
	

End Function 

Function bmxSetScale:Int (LuaState:Byte Ptr)
	Local xs:Float, ys:Float
	xs = lua_tonumber( LuaState, 1 )
	ys = lua_tonumber( LuaState, 2 )
	SetScale(xs,ys)
	Return 0
End Function

Function bmxSetOrigin:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	SetOrigin(x,y)
	Return 0
End Function

Function bmxline:Int (LuaState:Byte Ptr)
	Local x1:Float, y1:Float, x2:Float, y2:Float
	
	x1 = lua_tonumber( LuaState, 1 )
	y1 = lua_tonumber( LuaState, 2 )
	x2 = lua_tonumber( LuaState, 3 )
	y2 = lua_tonumber( LuaState, 4 )	
	DrawLine(x1,y1,x2,y2)
	
	Return 0
End Function


Function bmxsetcolor:Int (LuaState:Byte Ptr)
	Local r:Int = lua_tointeger( LuaState, 1 )
	Local g:Int = lua_tointeger( LuaState, 2 )
	Local b:Int = lua_tointeger( LuaState, 3 )
	SetColor(r,g,b)
	Return 0
End Function

Function bmxfillRect:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float, w:Float, h:Float
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	w = lua_tonumber( LuaState, 3 )
	h = lua_tonumber( LuaState, 4 )
	DrawRect(x,y,w,h)
	Return 0	
End Function

Function bmxdrawRect:Int (LuaState:Byte Ptr)
	Local sx:Float, sy:Float, wx:Float, wy:Float
	sx = lua_tonumber( LuaState, 1 )
	sy = lua_tonumber( LuaState, 2 )
	wx = lua_tonumber( LuaState, 3 )
	wy = lua_tonumber( LuaState, 4 )
	DrawLine(sx, sy,sx+wx, sy+0);
	DrawLine(sx, sy+wy,sx+wx, sy+wy);
	DrawLine(sx, sy, sx, sy+wy);
	DrawLine(sx+wx, sy, sx+wx, sy+wy);
	Return 0
End Function

Function bmxdrawsph:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float, size:Float
	
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	size = lua_tonumber( LuaState, 3 )
	DrawOval(x-size/2,y-size/2,size,size)
	
	Return 0
End Function

Function bmxprintf:Int (LuaState:Byte Ptr)
	Local x1:Float, y1:Float
	Local msg:String
	
	x1 = lua_tonumber( LuaState, 1 )
	y1 = lua_tonumber( LuaState, 2 )
	msg = lua_tostring( LuaState, 3 )	
	DrawText(msg,x1,y1)
	
	Return 0
End Function

Function bmxparticle:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float
	
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	SetBlend(LIGHTBLEND)
	DrawImage(particleImg,x-particleImg.width/2,y-particleImg.height/2)
	SetBlend(MASKBLEND)
	Return 0
End Function

Function bmxDrawSparkler:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float, size:Float
	
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	size = lua_tonumber( LuaState, 3 )
	SetBlend(LIGHTBLEND)
	For Local i:Int = 0 Until 32
		DrawImage(particleImg, x-particleImg.width/2+Rand(-size,size), y-particleImg.height/2+Rand(-size,size))
	Next
	SetBlend(MASKBLEND)
	Return 0
End Function

Function bmxPoly:Int (LuaState:Byte Ptr)
'DrawPoly( xy#[] )
	Return 0
End Function


Function bmxdrawsprite:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float, filename:String
	
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	filename = lua_tostring( LuaState, 3 )
	
	If MapContains(filenameSpriteMap,filename) 
		
		Local spr:TImage = TImage( MapValueForKey(filenameSpriteMap, filename) )
		'SetBlend(MASKBLEND)
		SetScale(3,3)
		DrawImage(spr, x, y)
		SetScale(1,1)
	Else
		'SetMaskColor(255,0,255)
		SetMaskColor(0,0,0)
		Local spr:TImage = LoadImage(filename)
		MidHandleImage(spr)
		'SetMaskColor(0,0,0)
		If spr = Null
		Else
			spriteList.addlast(spr) 
			Try
			MapInsert(filenameSpriteMap, filename, spr)
			Catch e:String
				Print e
			EndTry
		EndIf
	EndIf
	

	Return 0
End Function

Function bmxdrawAnimSprite:Int (LuaState:Byte Ptr)
	Local x:Float, y:Float, filename:String
	Local cell_width:Float, cell_height:Float, cell_count:Int, cell_frame:Int
	x = lua_tonumber( LuaState, 1 )
	y = lua_tonumber( LuaState, 2 )
	filename = lua_tostring( LuaState, 3 )
	cell_width = lua_tonumber( LuaState, 4 )
	cell_height = lua_tonumber( LuaState, 5 )
	cell_count = lua_tointeger( LuaState, 6 )
	cell_frame = lua_tointeger( LuaState, 7 )
	
	If MapContains(filenameSpriteMap,filename) 
		
		Local spr:TImage = TImage( MapValueForKey(filenameSpriteMap, filename) )
		If( cell_frame <= spr.frames.length )
			MidHandleImage(spr)
			SetScale(3,3)
			
			DrawImage(spr, x-spr.width/2, y-spr.height/2, cell_frame)
			SetScale(1,1)
		EndIf
	Else
		SetMaskColor(0,0,0)
		'TODO cath count size exceptions
		Local spr:TImage = LoadAnimImage(filename,cell_width,cell_height,0,cell_count,MASKEDIMAGE)
		
		If spr = Null
		Else
			spriteList.addlast(spr) 
			Try
			MapInsert(filenameSpriteMap, filename, spr)
			Catch e:String
				Print e
			EndTry
		EndIf
	EndIf
	

	Return 0
End Function



Type Vector3
	Field x#, y#, z#
End Type


Type DrawState
	Global movepos:Vector3 = new Vector3
	Global linepos:Vector3 = new Vector3
	
	
	
	
	Function bmxMoveTo:Int (LuaState:Byte Ptr)
		movepos.x = lua_tonumber( LuaState, 1 )
		movepos.y = lua_tonumber( LuaState, 2 )
		movepos.z = lua_tonumber( LuaState, 3 )
		Return 0
	End Function 
	
End Type

Function bmxLineTo:Int (LuaState:Byte Ptr)
	Local x# = lua_tonumber( LuaState, 1 )
	Local y# = lua_tonumber( LuaState, 2 )
	Local z# = lua_tonumber( LuaState, 3 )
	
	glDepthFunc(GL_LEQUAL)
	
	glMatrixMode(GL_PROJECTION)
	glpushmatrix() 
	setProjection(1)
	'glLoadIdentity()
	glDisable(GL_TEXTURE_2D)
	
	glBegin(GL_LINES)
	glvertex3f DrawState.movepos.x,DrawState.movepos.y,DrawState.movepos.z
	glvertex3f x,y,z
	'glvertex3f DrawState.linepos.x,DrawState.linepos.y,DrawState.linepos.z
	glEnd
	
	DrawState.movepos.x = x 
	DrawState.movepos.y = y
	DrawState.movepos.z = z

	
	glMatrixMode(GL_PROJECTION)
	glpopmatrix() 
	
End Function

